package com.phone580.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.phone580.app.login.LoginActivity;
import com.phone580.app.untilty.DCExit;

public class BaseActivity extends Activity{
	
	protected final static String APP = "APP";
	protected final static String GAME = "GAME";
	protected int ptypeId = -1;
	protected String type = "";
	protected WebView web;

	protected void setType(String type) {
		this.type = type;
		if (APP.equals(type)) {
			ptypeId = 1;
		} else {
			ptypeId = 2;
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	protected void initHeader() {
		//热门推荐
		final Button btnHeaderRecommand = (Button) findViewById(R.id.header_recommand_btn);
		//选中状态
		btnHeaderRecommand.setBackgroundResource(R.drawable.header_item_selected);
		//装机宝典
		final Button btnHeaderIntall = (Button) findViewById(R.id.header_intall_btn);
		//分类
		final Button btnHeaderFilter = (Button) findViewById(R.id.header_filter_btn);
		//搜索
		final Button btnHeaderSearch = (Button) findViewById(R.id.header_search_btn);
		//会员登录
		final Button btnHeaderLogin = (Button) findViewById(R.id.header_login_btn);
		btnHeaderLogin.setVisibility(View.VISIBLE);
		//会员名称
		final TextView tvUserName = (TextView) findViewById(R.id.header_login_username);
		tvUserName.setVisibility(View.INVISIBLE);
		
		btnHeaderRecommand.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//改变背景，选中为黄色背景
				btnHeaderRecommand
						.setBackgroundResource(R.drawable.header_item_selected);
				btnHeaderIntall
						.setBackgroundResource(R.drawable.header_item_selector);
				btnHeaderFilter
						.setBackgroundResource(R.drawable.header_item_selector);
				//加载list页面
//				web.loadUrl("file:///mnt/sdcard/bak2/" + "list.html?appType=" + type);
				web.loadUrl(CommonData.DATA_PATH + "list.html?appType=" + type);
			}
		});

		btnHeaderIntall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//改变背景，选中为黄色背景
				btnHeaderIntall
						.setBackgroundResource(R.drawable.header_item_selected);
				btnHeaderRecommand
						.setBackgroundResource(R.drawable.header_item_selector);
				btnHeaderFilter
						.setBackgroundResource(R.drawable.header_item_selector);
				//加载recommend页面
//				web.loadUrl("file:///mnt/sdcard/bak2/" + "recommend.html?catalog=" + type);
				web.loadUrl(CommonData.DATA_PATH + "recommend.html?catalog=" + type);
			}
		});

		btnHeaderFilter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//改变背景，选中为黄色背景
				btnHeaderFilter
						.setBackgroundResource(R.drawable.header_item_selected);
				btnHeaderRecommand
						.setBackgroundResource(R.drawable.header_item_selector);
				btnHeaderIntall
						.setBackgroundResource(R.drawable.header_item_selector);
				//加载category页面
//				web.loadUrl("file:///mnt/sdcard/bak2/" + "category.html?ptypeId=" + ptypeId);
				web.loadUrl(CommonData.DATA_PATH + "category.html?ptypeId=" + ptypeId);
			}
		});
		
		btnHeaderSearch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				web.loadUrl("file:///mnt/sdcard/bak2/" + "search.html");
				web.loadUrl(CommonData.DATA_PATH + "search.html");
				btnHeaderFilter
						.setBackgroundResource(R.drawable.header_item_selector);
				btnHeaderRecommand
						.setBackgroundResource(R.drawable.header_item_selector);
				btnHeaderIntall
						.setBackgroundResource(R.drawable.header_item_selector);
			}
		});
		
		btnHeaderLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});
	}
	
	protected void initHeader (String title) {
		final ImageView ivBack = (ImageView) findViewById(R.id.header_back_iv);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		final TextView tvTitle = (TextView) findViewById(R.id.header_title_tv);
		tvTitle.setText(title);
	}
	
	protected void showNetworkError() {
		Toast.makeText(this, "网络发生错误", Toast.LENGTH_SHORT).show();
	}
	
	public void userLogonOrLogout() {
		final TextView tvUserName = (TextView) findViewById(R.id.header_login_username);
		final Button btnHeaderLogin = (Button) findViewById(R.id.header_login_btn);
		if (CommonData.login != null) {
			tvUserName.setVisibility(View.VISIBLE);
			btnHeaderLogin.setVisibility(View.INVISIBLE);
			tvUserName.setText(CommonData.login.getUsername());
		} else {
			tvUserName.setVisibility(View.INVISIBLE);
			btnHeaderLogin.setVisibility(View.VISIBLE);
		}
	}
	
	protected void initOtherHeader() {
		//会员登录
				final Button btnHeaderLogin = (Button) findViewById(R.id.header_login_btn);
				btnHeaderLogin.setVisibility(View.VISIBLE);
				//会员名称
				final TextView tvUserName = (TextView) findViewById(R.id.header_login_username);
				tvUserName.setVisibility(View.INVISIBLE);
				
				btnHeaderLogin.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
						startActivity(intent);
					}
				});
				
				final ImageView ivBack = (ImageView) findViewById(R.id.header_back_iv);
				ivBack.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						finish();
					}
				});
	}
	
	DCExit	exit	= new DCExit();

	/**
	 * 再按一次退出程序。
	 */
	protected void pressAgainExit() {
		if (exit.isExit()) {
			((MainApplication)this.getApplication()).saveDownloadList();
			((MainApplication)this.getApplication()).cleanAllNotification();
			System.exit(0);
		} else {
			Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			exit.doExitInOneSecond();
		}
	}
	
}
